package com.atqar.lock;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ziyuann{

    Lock lock1=new ReentrantLock();
    Lock lock2=new ReentrantLock();
    String falg="1";
    String falg2="2";

    public void method(){
        synchronized (falg){
            System.out.println(Thread.currentThread().getName()+"完成了");
            synchronized (falg2){
                System.out.println("枪锁");
            }
        }
    }
    public void method2(){
        synchronized (falg2){
            System.out.println(Thread.currentThread().getName()+"完成了");
            synchronized (falg){
                System.out.println("枪锁");
            }
        }
    }




/*
    public void method(){

        lock1.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"执行");
            lock2.lock();
            try {
                System.out.println("抢到2锁");
            }finally {
                lock2.unlock();
            }
        }finally {
            lock1.unlock();
        }

    }
    public void method2(){

        lock2.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"执行");
            lock1.lock();
            try {
                System.out.println("抢到2锁");
            }finally {
                lock1.unlock();
            }
        }finally {
            lock2.unlock();
        }

    }*/

}



public class DealockDemo {
    public static void main(String[] args) {
        Ziyuann zy = new Ziyuann();
        new Thread(()->{
            zy.method();
        }).start();

        new Thread(()->{
            zy.method2();
        }).start();

    }
}








/*
class zy1{
    public static synchronized void method1(){
        zy2 zy2 = new zy2();
        System.out.println(Thread.currentThread().getName()+"完成");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        zy2.method1();
        System.out.println("枪锁");
    }
}

class zy2{
    public static synchronized void method1(){
        zy1 zy1 = new zy1();
        System.out.println(Thread.currentThread().getName()+"完成");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        zy1.method1();
        System.out.println("枪锁");

    }
}




public class DealockDemo {
    public static void main(String[] args) {
        zy2 zy2 = new zy2();
        zy1 zy1 = new zy1();
        new Thread(()->{
            zy1.method1();
        }).start();

        new Thread(()->{
            zy2.method1();
        }).start();

    }
}
*/
